// Package util funcs for other packages
package util

import (
	"encoding/binary"
)

// BytesToUint16 : convert every 2 bytes to uint16
func BytesToUint16(in []byte) []uint16 {
	var arr []uint16
	if len(in) == 2 {
		arr = append(arr, binary.LittleEndian.Uint16(in))
	} else {
		for i := 0; i < len(in); i += 2 {
			item := binary.LittleEndian.Uint16(in[i : i+2])
			arr = append(arr, item)
		}
	}
	return arr
}

// BytesToInt16 : convert every 2 bytes to int16
func BytesToInt16(in []byte) []int16 {
	var arr []int16
	if len(in) == 2 {
		arr = append(arr, int16(binary.LittleEndian.Uint16(in)))
	} else {
		for i := 0; i < len(in); i += 2 {
			item := binary.LittleEndian.Uint16(in[i : i+2])
			arr = append(arr, int16(item))
		}
	}
	return arr
}

// Uint16ToInt16 converts []uint16 to []int16
func Uint16ToInt16(in []uint16) []int16 {
	out := []int16{}
	for _, item := range in {
		out = append(out, int16(item))
	}
	return out
}

// ExtractReflectivity extract 11 bits as reflectivity from a uint16
func ExtractReflectivity(in uint16) uint16 {
	out := in >> 4
	return out
}

// Check error
func Check(err error) {
	if err != nil {
		panic(err)
	}
}
